package com.yjian.dao.provider;

import com.yjian.dao.bean.Member;
import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.jdbc.SQL;
import springfox.documentation.service.ApiListing;

import java.util.Map;

/**
 * Created by xuxiaobao on 2017/9/27.
 */
public class MemberProvider {

    /**
     * 获取店家会员列表
     * @param member
     * @return
     */
    public String getMemberListByUid(final Member member) {
        return new SQL(){{
            SELECT(member.getQuery_sql());
            FROM("t_member");
            WHERE("uid=#{uid}");
            ORDER_BY("createTime");
        }}.toString();
    }

    /**
     * 获取会员信息
     * @param member
     * @return
     */
    public String getMemberByMid(final Member member) {
        return new SQL(){{
            SELECT(member.getQuery_sql());
            FROM("t_member");
            WHERE("mid=#{mid}");
        }}.toString();
    }

    /**
     * 保存店家会员
     * @param member
     * @return
     */
    public String saveMember(final Member member) {
        return new SQL(){{
            INSERT_INTO("t_member");
            if (StringUtils.isNotEmpty(member.getMid()))  {
                VALUES("mid",member.getMid());
            }
            if (StringUtils.isNotEmpty(member.getUid())) {
                VALUES("uid", "#{uid}");
            }
            if (StringUtils.isNotEmpty(member.getPhone())) {
                VALUES("phone", "#{phone}");
            }
            if (StringUtils.isNotEmpty(member.getUsername())) {
                VALUES("username","#{username}");
            }
            if (member.getBalance() != null) {
                VALUES("balance","#{balance}");
            }
            if (member.getNumber() != null) {
                VALUES("number", "#{number}");
            }
            if (member.getCreateTime() != null) {
                VALUES("createTime", "#{createTime}");
            }
        }}.toString();
    }

    /**
     * 更新店家会员
     * @param member
     * @return
     */
    public String updateMemberByMid(final Member member) {
        SQL sql = new SQL();
        sql.UPDATE("t_member");
        if (StringUtils.isNotEmpty(member.getPhone())) {
            sql.SET("phone=#{phone}");
        }
        if (StringUtils.isNotEmpty(member.getUsername())) {
            sql.SET("username=#{username}");
        }
        if (member.getBalance() != null) {
            sql.SET("balance=#{balance}");
        }
        if (member.getNumber() != null) {
            sql.SET("number=#{number}");
        }
        return sql.toString();
    }

    /**
     * 查询店铺是否存在该会员
     * @param member
     * @return
     */
    public String isExistMemberAndUser(final Member member) {
        SQL sql = new SQL();
        sql.SELECT("count(1)");
        sql.FROM("t_member");
        sql.WHERE("uid=#{uid} AND mid=#{mid}");

        return sql.toString();
    }

    /**
     * 根据手机号搜索店铺下的会员信息
     * @param member
     * @return
     */
    public String getMemberByPhone(final Member member) {
        SQL sql = new SQL();
        sql.SELECT(member.getQuery_sql());
        sql.FROM("t_member");
        sql.WHERE("uid=#{uid}");
        sql.AND();
        sql.WHERE("phone like  CONCAT('%',#{phone},'%')");
        return sql.toString();
    }

    /**
     * 根据手机号，会员名查询用户下的会员总数
     * @param member
     * @return
     */
    public String getMemberTotalByCondition(final Member member) {
        SQL sql = new SQL();
        sql.SELECT("count(1)");
        sql.FROM("t_member");
        sql.WHERE("1=1");
        if (StringUtils.isNotEmpty(member.getUid())) {
            sql.AND();
            sql.WHERE("uid=#{uid}");
        }
        if (StringUtils.isNotEmpty(member.getPhone())) {
            sql.AND().WHERE("phone LIKE CONCAT('%',#{phone},'%')");
        }
        if (StringUtils.isNotEmpty(member.getUsername())) {
            sql.AND().WHERE("username LIKE CONCAT('%',#{username},'%')");
        }
        return sql.toString();
    }

    /**
     * 通过手机号,会员名搜索会员信息
     * @param member
     * @return
     */
    public String getMemberPageByCondition(Member member) {
        SQL sql = new SQL();
        sql.SELECT(member.getQuery_sql());
        sql.FROM("t_member");
        sql.WHERE("1=1");
        if (StringUtils.isNotEmpty(member.getUid())) {
            sql.AND().WHERE("uid=#{uid}");
        }
        if (StringUtils.isNotEmpty(member.getPhone())) {
            sql.AND().WHERE("phone LIKE CONCAT('%',#{phone},'%')");
        }
        if (StringUtils.isNotEmpty(member.getUsername())) {
            sql.AND().WHERE("username LIKE CONCAT('%',#{username},'%')");
        }
        return sql.toString() + " LIMIT #{offSet},#{pageSize}";
    }

    /**
     * 查询客户是否有该会员
     * @param member
     * @return
     */
    public String isExistMemberByPhone(final Member member) {
        SQL sql = new SQL();
        sql.SELECT("count(1)");
        sql.FROM("t_member");
        sql.WHERE("uid=#{uid}");
        sql.AND();
        sql.WHERE("phone=#{phone}");
        return sql.toString();
    }

    public String selectExecute(final Map<String, Object> map) {
        String sql = map.get("querySql").toString();
        return sql;
    }
}
